package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacDrugResultSenrole;
import com.ysd.lis.entity.bac.BacDrugResultSenroleDetails;
import com.ysd.lis.entity.bac.BacInterpretationCodeDict;
import com.ysd.lis.entity.bac.BacSensitiveResultRule;
import com.ysd.lis.mapper.bac.BacDrugResultSenroleDetailsMapper;
import com.ysd.lis.mapper.bac.BacDrugResultSenroleMapper;
import com.ysd.lis.service.bac.BacDrugResultSenroleDetailsService;
import com.ysd.lis.service.bac.BacDrugResultSenroleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-04-26
 */
@Service
public class BacDrugResultSenroleServiceImpl extends ServiceImpl<BacDrugResultSenroleMapper, BacDrugResultSenrole> implements BacDrugResultSenroleService {

    @Autowired
    private BacDrugResultSenroleDetailsService bacDrugResultSenroleDetailsService;
    @Override
    public Result findSenroleList() {
        MPJLambdaWrapper<BacDrugResultSenrole> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacDrugResultSenrole.class)
                .selectAs(BacInterpretationCodeDict::getName, BacDrugResultSenrole::getSensitiveNa)
                .leftJoin(BacInterpretationCodeDict.class, "sa",
                        p -> p.eq(BacInterpretationCodeDict::getCode, BacDrugResultSenrole::getSensitiveNo).eq(BacInterpretationCodeDict::getDelFlag, 0));
        queryWrapper.eq(BacDrugResultSenrole::getDelFlag, 0)
                .orderByAsc(BacDrugResultSenrole::getSeq);
        List<BacDrugResultSenrole> ruleList = this.baseMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", ruleList);
    }

    @Override
    public Result saveOrUpdateSenrole(List<BacDrugResultSenrole> list) {
        boolean b = this.saveOrUpdateBatch(list);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDeleteSenroleRule(List<BacDrugResultSenrole> list) {
        List<String> ids = list.stream().map(BacDrugResultSenrole::getId).collect(Collectors.toList());
        List<String> roleCodeList = list.stream().map(BacDrugResultSenrole::getRoleCode).collect(Collectors.toList());
        bacDrugResultSenroleDetailsService.remove(new LambdaQueryWrapper<BacDrugResultSenroleDetails>().in(BacDrugResultSenroleDetails::getRoleCode, roleCodeList));
        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }
}
